package com.tjsinfo.tjsoa.address.personal;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import org.hibernate.Session;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;


//import com.tjsinfo.tjsoa.address.common.TAddresslistCommon;

/**
 * A data access object (DAO) providing persistence and search support for
 * TAddresslistPersonal entities. Transaction control of the save(), update()
 * and delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.tjsinfo.oa.addresslist.personal.TAddresslistPersonal
 * @author MyEclipse Persistence Tools
 */

public class TAddresslistPersonalDao extends HibernateDaoSupport implements ITAddresslistPersonalDao {
	
	private static final Log log = LogFactory.getLog(TAddresslistPersonalDao.class);
	public void save(TAddresslistPersonal transientInstance) {
		log.debug("saving TAddresslistPersonal instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(TAddresslistPersonal persistentInstance) {
		log.debug("deleting TAddresslistPersonal instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public void delete(String propertyName, Object value) {
		log.debug("deleting TAddresslistPersonal instance");
		try {
			String hql="delete TAddresslistPersonal where "+propertyName+" = "+value;
			getHibernateTemplate().delete(value);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}
	
	public void update(TAddresslistPersonal persistentInstance){
		log.debug("saving TAddresslistPersonal instance");
		try {
			this.getHibernateTemplate().update(persistentInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	};
	
	public List findById(java.lang.Integer id) {
	    log.debug("finding TAddresslistPersonal instance with id: "
			+ id );
	    try {
		    String queryString = "from TAddresslistPersonal as model where model.id = ? order by id desc";
		    return getHibernateTemplate().find(queryString, id);
	    } catch (RuntimeException re) {
		    log.error("find by property name failed", re);
		    throw re;
	    }
	}

	public TAddresslistPersonal findByID(java.lang.Integer id) {
		Session session=getSession();
		log.debug("getting TAddresslistPersonal instance with id: " + id);
		try {
			TAddresslistPersonal instance = (TAddresslistPersonal) getHibernateTemplate()
			        .get("com.tjsinfo.tjsoa.address.personal.TAddresslistPersonal",id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public List findByExample(TAddresslistPersonal instance) {
		log.debug("finding TAddresslistPersonal instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding TAddresslistPersonal instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from TAddresslistPersonal as model where model."
					+ propertyName + "= ? order by id desc";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findAll() {
		log.debug("finding all TAddresslistPersonal instances");
		try {
			String queryString = "from TAddresslistPersonal";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public List findByLikePropertyAndProperty(String likeName, Object value, String propertyName,Object values) {
		Session session=getSession();
		log.debug("finding TAddresslistPersonal instance with property: "
				+ likeName + ", value: " + value + ", property:" + propertyName +",value:"+values);
		try {
			Criteria criteria = session.createCriteria(TAddresslistPersonal.class);
			criteria.add(Expression.like(likeName, value));
			criteria.add(Expression.like(propertyName, values));
			return criteria.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	public List findByLikeProperty(String likeName, Object value) {
		Session session=getSession();
		log.debug("finding TAddresslistPersonal instance with property: "
				+ likeName + ", value: " + value);
		try {
			Criteria criteria = session.createCriteria(TAddresslistPersonal.class);
			criteria.add(Expression.like(likeName, value));
			return criteria.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	public List findByLikeProperty(String likeName, Object value, String propertyName,Object values, String propertyid, Object valueid){
		Session session=getSession();
		log.debug("finding TAddresslistPersonal instance with property:"
				+ likeName + ", value: " + value + ", property:" + propertyName +",value:"+values+",property:"+propertyid+",value:"+valueid);
		try {
			Criteria criteria = session.createCriteria(TAddresslistPersonal.class);
			criteria.add(Expression.like(likeName, value));
			criteria.add(Expression.like(propertyName, values));
			criteria.add(Expression.like(propertyid, valueid));
			return criteria.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	public List findList(String chineseName, Object value, String department,Object values, String userId,Object valuess) {
		Session session=getSession();
		log.debug("finding TAddresslistInside instance with property: "
				+ chineseName + ", value: " + value + ", property:" + department +",value:"+values + ", property:" + userId +",value:"+valuess);
		try {
			Criteria criteria = session.createCriteria(TAddresslistPersonal.class);
			criteria.add(Expression.like(chineseName, value));
			criteria.add(Expression.like(department, values));
			criteria.add(Expression.eq(userId, valuess));
			criteria.addOrder(Order.desc("id"));
			return criteria.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
}